package com.allwees.bs.core.modelbase.req;

import com.allwees.bs.core.modelbase.branch.BranchUser;
import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.bs.core.modelbase.exception.AuthException;
import com.allwees.bs.core.modelbase.exception.AuthExceptionEnum;
import com.allwees.bs.core.modelbase.validate.Validatable;
import com.allwees.bs.core.modelbase.validate.ValidateItem;
import com.allwees.core.common.util.DateUtil;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * 封装数据查询对象

 * @author: yangylsky
 */
@Data
@ToString
public abstract class Req implements Serializable, Validatable {
	private static final Date TIME_FLOOR = DateUtil.getDate(2019, 9, 1);

	private static final Date TIME_UPPER = DateUtil.getDate(2070, 0, 1);

	/**
	 * 当前登录关联机构用户，由拦截器注入
	 */
	private BranchUser branchUser;

	protected Date time = DateUtil.now();

	@Override
	public ValidateItem[] getValidateItems() {
		return new ValidateItem[0];
	}

	protected boolean isValidTime(Date time) {
		return time != null && TIME_FLOOR.before(time) && TIME_UPPER.after(time);
	}

	/**
	 * 自动补全字段（需在调用Validatable.validate()通过后调用）
	 */
	protected void completion() {
	}

	@Override
	public void afterValidate() {
		completion();
	}

	public boolean isValidBranchUser() {
		return branchUser != null && StringUtils.isNotBlank(branchUser.getBranchUuid());
	}

	public BranchUser checkBranchUser() {
		if(branchUser == null) {
			throw new AuthException(AuthExceptionEnum.NO_BRANCH_USER);
		}
		if(StringUtils.isBlank(branchUser.getBranchUuid())) {
			throw new AuthException(AuthExceptionEnum.NO_BRANCH_ID);
		}
		return branchUser;
	}

	public String getBranchUserUuid() {
		return checkBranchUser().getUuid();
	}

	public EBranchType getBranchType() {
		return checkBranchUser().getBranchType();
	}

	public String getBranchUuid() {
		return checkBranchUser().getBranchUuid();
	}

	public String getSupplierUuid() {
		return branchUser.getCurrentSupplierUuid();
	}

	public String getRetailerUuid() {
		return branchUser.getCurrentRetailerUuid();
	}

	public String getStoreUuid() {
		return branchUser.getStoreUuid();
	}

	public String getPlatformUuid() {
		return branchUser.getCurrentPlatformUuid();
	}

	public String getCurrentSupplierUuid() {
		return branchUser.getCurrentSupplierUuid();
	}

	public String getCurrentRetailerUuid() {
		return branchUser.getCurrentRetailerUuid();
	}

	public String getCurrentStoreUuid() {
		return branchUser.getStoreUuid();
	}

	public String getCurrentPlatformUuid() {
		return branchUser.getCurrentPlatformUuid();
	}
}
